<?php

namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;

/**
 * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\User>
 */
class UserFactory extends Factory
{
    /**
     * The current password being used by the factory.
     */
    protected static ?string $password;

    /**
     * Define the model's default state.
     *
     * @return array<string, mixed>
     */
    public function definition(): array
    {
        // 随机决定使用邮箱还是手机号
        $useEmail = fake()->boolean(70); // 70% 概率使用邮箱
        
        return [
            'name' => fake()->name(),
            'email' => $useEmail ? fake()->unique()->safeEmail() : null,
            'phone' => !$useEmail ? fake()->unique()->numerify('1##########') : null,
            'email_verified_at' => $useEmail ? now() : null,
            'password' => static::$password ??= Hash::make('password'),
            'remember_token' => Str::random(10),
        ];
    }

    /**
     * Indicate that the model's email address should be unverified.
     */
    public function unverified(): static
    {
        return $this->state(fn (array $attributes) => [
            'email_verified_at' => null,
        ]);
    }

    /**
     * 创建只有邮箱的用户
     */
    public function emailOnly(): static
    {
        return $this->state(fn (array $attributes) => [
            'email' => fake()->unique()->safeEmail(),
            'phone' => null,
            'email_verified_at' => now(),
        ]);
    }

    /**
     * 创建只有手机号的用户
     */
    public function phoneOnly(): static
    {
        return $this->state(fn (array $attributes) => [
            'email' => null,
            'phone' => fake()->unique()->numerify('1##########'),
            'email_verified_at' => null,
        ]);
    }

    /**
     * 创建同时有邮箱和手机号的用户
     */
    public function withBothContacts(): static
    {
        return $this->state(fn (array $attributes) => [
            'email' => fake()->unique()->safeEmail(),
            'phone' => fake()->unique()->numerify('1##########'),
            'email_verified_at' => now(),
        ]);
    }

    /**
     * 创建管理员用户
     */
    public function admin(): static
    {
        return $this->state(fn (array $attributes) => [
            'name' => 'Apple Store 管理员',
            'email' => 'admin@example.com',
            'phone' => '13146148179',
            'email_verified_at' => now(),
        ]);
    }
}
